package in.muton.edushare.components.messaging.recievepanel;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.messaging.api.AssignmentMessage;
import in.muton.edushare.panels.images.ImageWithLink;
import in.muton.edushare.schedules.widgets.DateLabel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.ExternalLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.AssignmentsDAO;
import com.edushare.common.dao.FileDirectoriesDAO;
import com.edushare.common.dao.FilesUploadedDAO;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.FileDirectories;
import com.edushare.common.vo.FilesUploaded;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.interfaces.IFileUploadable;

public class AssignmentMessagePanel extends Panel {

	public AssignmentMessagePanel(String id, IModel<Messages> model) {
		super(id, model);
		this.msg = model;
		getAssignment();
		
		add(new ImageWithLink(
					"senderImage", 
					new CayenneModel<IFileUploadable>(assgn.getObject().getCourseInstance().getCourse()), 
					ApplicationConstants.FILE_SUFFIX_ICON));
		
		add(new EduShareURLPanel("name", new CayenneModel<CourseInstances>(
				assgn.getObject().getCourseInstance())));
		add(new Label("msg", message).setEscapeModelStrings(false));
		add(getTable());
		add(new DateLabel("createDate", assgn.getObject().getStartDate(), 0));
		PageParameters params = new PageParameters();
		params.add("0", assgn.getObject().getAssignmentId().toString());
		add(new BookmarkablePageLink<Void>("assgnLink", URLLoader
				.getPageClass(URLConstants.ASSIGNMENT_PATH), params));
	}

	public Fragment getTable() {
		if (assgnType == AssignmentMessage.ASSIGNMENT_CANCELLED) {
			return new Fragment("table", "dummy", this);
		}

		Fragment f = new Fragment("table", "assDetails", this);
		if (assgnType == AssignmentMessage.NEW_ASSIGNMENT_CREATED
				|| assgnType == AssignmentMessage.ASSIGNMENT_DATES_CHANGED
				|| assgnType == AssignmentMessage.ASSIGNMENT_TITLE_MODIFIED) {
			WebMarkupContainer wmc = new WebMarkupContainer("opendate");
			wmc.add(new DateLabel("open", assgn.getObject().getStartDate(), 0));
			f.add(wmc);
			wmc = new WebMarkupContainer("closedate");
			wmc.add(new DateLabel("close", assgn.getObject().getEndDate(), 0));
			f.add(wmc);
			if (assgnType == AssignmentMessage.ASSIGNMENT_TITLE_MODIFIED) {
				WebMarkupContainer w1;
				wmc = new WebMarkupContainer("fileWMC");
				wmc.add(w1 = new WebMarkupContainer("filelist"));
				w1.add(new WebMarkupContainer("file"));
				f.add(wmc);
				return f;
			}
		}

		WebMarkupContainer wmc = new WebMarkupContainer("fileWMC");
		FileDirectoriesDAO fileDirectoryDAO = new FileDirectoriesDAO(
				EduShareSession.get().getSessionORMInterface());
		FilesUploadedDAO filesUploadedDAO = new FilesUploadedDAO(
				EduShareSession.get().getSessionORMInterface());
		final FileDirectories fileDirectory = fileDirectoryDAO
				.getOrCreateDefaultDirectory(assgn.getObject());
		List<FilesUploaded> afs = filesUploadedDAO.getFilesUploaded(
				fileDirectory,
				ApplicationConstants.FILE_DIR_PERMISSION_TYPE_PUBLIC);
		wmc.add(new ListView<FilesUploaded>("filelist", afs) {
			private static final long serialVersionUID = -4157194446943021276L;

			@Override
			protected void populateItem(ListItem<FilesUploaded> arg0) {
				FilesUploaded af = arg0.getModelObject();
				arg0.add(new ExternalLink("file", af.getFileDownloadUrl(), af
						.getFileOrigName()));
			}
		});
		f.add(wmc);
		return f;
	}

	public void getAssignment() {
		String str = msg.getObject().getMessage();
		if (str.contains(AssignmentMessage.NEW_ASSIGNMENT_CREATED)) {
			assgnType = AssignmentMessage.NEW_ASSIGNMENT_CREATED;
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_TITLE_MODIFIED)) {
			assgnType = AssignmentMessage.ASSIGNMENT_TITLE_MODIFIED;
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_FILES_UPLOADED)) {
			assgnType = AssignmentMessage.ASSIGNMENT_FILES_UPLOADED;
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_DATES_CHANGED)) {
			assgnType = AssignmentMessage.ASSIGNMENT_DATES_CHANGED;
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_CANCELLED)) {
			assgnType = AssignmentMessage.ASSIGNMENT_CANCELLED;
		}

		int startIndex = ("<<<EDUSHAREMESSAGINGMETADATA:ASSIGNMENT:"
				+ assgnType + ":").length();
		int endIndex = str.indexOf(">>>", startIndex);
		int assgnId = Integer.parseInt(str.substring(startIndex, endIndex));
		assgn = new CayenneModel<Assignments>(new AssignmentsDAO(
				EduShareSession.get().getSessionORMInterface())
				.getAssignmentByAssignmentId(assgnId));

		if (str.contains(AssignmentMessage.NEW_ASSIGNMENT_CREATED)) {
			message = "A new assignment <strong>'"
					+ assgn.getObject().getAssignmentTitle() + "'</strong> "
					+ "have been created. <br/>";
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_TITLE_MODIFIED)) {
			message = "New files have been added to the <strong>'"
					+ assgn.getObject().getAssignmentTitle() + "'</strong> "
					+ "have been modified. <br/>";
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_FILES_UPLOADED)) {
			message = "New files have been added to the  <strong>'"
					+ assgn.getObject().getAssignmentTitle() + "'</strong> "
					+ "have been modified. <br/>";
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_DATES_CHANGED)) {
			message = "The dates for assignment <em>'"
					+ assgn.getObject().getAssignmentTitle() + "'</strong> "
					+ "have been modified. <br/>";
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_CANCELLED)) {
			message = "The assignment <strong>'"
					+ assgn.getObject().getAssignmentTitle() + "'</strong> "
					+ "has been cancelled.";
		}

	}
	
	public static boolean exists(String str){
		String assgnType = "";
		if (str.contains(AssignmentMessage.NEW_ASSIGNMENT_CREATED)) {
			assgnType = AssignmentMessage.NEW_ASSIGNMENT_CREATED;
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_TITLE_MODIFIED)) {
			assgnType = AssignmentMessage.ASSIGNMENT_TITLE_MODIFIED;
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_FILES_UPLOADED)) {
			assgnType = AssignmentMessage.ASSIGNMENT_FILES_UPLOADED;
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_DATES_CHANGED)) {
			assgnType = AssignmentMessage.ASSIGNMENT_DATES_CHANGED;
		} else if (str.contains(AssignmentMessage.ASSIGNMENT_CANCELLED)) {
			assgnType = AssignmentMessage.ASSIGNMENT_CANCELLED;
		}else
			return false;

		int startIndex = ("<<<EDUSHAREMESSAGINGMETADATA:ASSIGNMENT:"
				+ assgnType + ":").length();
		int endIndex = str.indexOf(">>>", startIndex);
		int assgnId = Integer.parseInt(str.substring(startIndex, endIndex));
		if(new AssignmentsDAO(
				EduShareSession.get().getSessionORMInterface())
				.getAssignmentByAssignmentId(assgnId) == null)
			return false;
		else 
			return true;
	}

	private String message = "";
	private String assgnType = "";
	private IModel<Assignments> assgn = null;
	private IModel<Messages> msg = null;
	private static final long serialVersionUID = 4463452699022656413L;
}
